Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

core/data/src/commonMain/kotlin/org/meshtastic/core/data/manager/RequestTimer.kt 2d20cd8a4708e2ef66e98d5259f3a97ec93f240a (2d20cd8a) Text, 2.48 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.data.manager

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.atomicfu.atomic
Tff7b72import T7ee787kotlinx.atomicfu.update
Tff7b72import T7ee787kotlinx.collections.immutable.persistentMapOf
Tff7b72import T7ee787org.meshtastic.core.common.util.NumberFormatter
Tff7b72import T7ee787org.meshtastic.core.common.util.nowMillis

T8b949e/**
* Tracks per-request start times and reports round-trip durations for request/response handlers.
*
* Request handlers (traceroute, neighbor-info, …) call [start] when issuing a request keyed by its id, then
* [appendDuration] when the matching response arrives to annotate the user-facing text with how long the round trip
* took. Start times are stored in an atomic immutable map so [start] (any coroutine) and [appendDuration] (the handler
* scope) never race.
*/
Tff7b72internal Tff7b72class T56d364RequestTimer Tb4b4b4{

Tff7b72private Tff7b72val Te6edf3startTimes Tff7b72= Te6edf3atomicTb4b4b4(Te6edf3persistentMapOfTff7b72<Tffa657IntTb4b4b4, Tffa657LongTff7b72>Tb4b4b4(Tb4b4b4)Tb4b4b4)

T8b949e/** Records the start time for [requestId]. */
Tff7b72fun Td2a8ffstartTb4b4b4(Te6edf3requestIdTb4b4b4: Tffa657IntTb4b4b4) Tb4b4b4{
Te6edf3startTimesTb4b4b4.Te6edf3update Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3puttingTb4b4b4(Te6edf3requestIdTb4b4b4, Te6edf3nowMillisTb4b4b4) Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Consumes the start time recorded for [requestId] and appends a `Duration: N s` line to [text], logging completion
* under [logLabel]. Returns [text] unchanged when no start time was recorded for the id.
*/
Tff7b72fun Td2a8ffappendDurationTb4b4b4(Te6edf3requestIdTb4b4b4: Tffa657IntTb4b4b4, Te6edf3textTb4b4b4: Tffa657StringTb4b4b4, Te6edf3logLabelTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Tffa657String Tb4b4b4{
Tff7b72val Te6edf3start Tff7b72= Te6edf3startTimesTb4b4b4.Te6edf3valueTff7b72[Te6edf3requestIdTff7b72]
Te6edf3startTimesTb4b4b4.Te6edf3update Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3removingTb4b4b4(Te6edf3requestIdTb4b4b4) Tb4b4b4}
Tff7b72if Tb4b4b4(Te6edf3start Tff7b72=Tff7b72= Tff7b72nullTb4b4b4) Tff7b72return Te6edf3text
Tff7b72val Te6edf3seconds Tff7b72= Tb4b4b4(Te6edf3nowMillis Tff7b72- Te6edf3startTb4b4b4) Tff7b72/ Te6edf3MILLIS_PER_SECOND
Te6edf3LoggerTb4b4b4.Te6edf3i Tb4b4b4{ Ta5d6ff"Tffd700$Te6edf3logLabelTa5d6ff Tffd700$Te6edf3requestIdTa5d6ff complete in Tffd700$Te6edf3secondsTa5d6ff sTa5d6ff" Tb4b4b4}
Tff7b72return Ta5d6ff"Tffd700$Te6edf3textTa5d6ff\Ta5d6ffnTa5d6ff\Ta5d6ffnDuration: Tffd700${Te6edf3NumberFormatterTb4b4b4.Te6edf3formatTb4b4b4(Te6edf3secondsTb4b4b4, T79c0ff1Tb4b4b4)Tffd700}Ta5d6ff sTa5d6ff"
Tb4b4b4}

Tff7b72private Tff7b72companion Tff7b72object Tb4b4b4{
Tff7b72private Tff7b72const Tff7b72val Te6edf3MILLIS_PER_SECOND Tff7b72= T79c0ff1T79c0ff0T79c0ff0T79c0ff0.0
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.1s